package leetcode.code0096;

public class Solution extends Solution96 {

	@Override
	public int numTrees(int n) {
		int[][] dp = new int[n + 2][n + 2];
		return this.f(1, n, dp);
	}

	private int f(int l, int r, int[][] dp) {
		if (l >= r) {
			dp[l][r] = 1;
			return 1;
		}
		if (dp[l][r] != 0) {
			return dp[l][r];
		}
		int ans = 0;
		for (int i = l; i <= r; i++) {
			ans += this.f(l, i - 1, dp) * this.f(i + 1, r, dp);
		}
		dp[l][r] = ans;
		return ans;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.numTrees(10);
	}

}
